/*
 * Hàm tải trang .html
 */
function load_page(url, container,typeSlider) {
    $(".page_active").removeClass("page_active").addClass("page_hide");
    container.addClass("page_active").removeClass("page_hide");
    
    /*
     *@Tra #page ve luc hide sidebar
     */
    //    var state = snapper.state();
    //    
    //    if(state.state=="left" || state.state=="right"){
    //        snapper.close();
    //    }
        
    if(typeSlider==="slideNext"){
        // alert("next");
        $(".page_active").css({
            "transition": "0.5s",
            "-webkit-transition": "0.5s",
            "-webkit-transform": "translate3d(0px, 0px, 0px)",
            "-o-transform": "translate3d(0px,0px,0px)",
            "-ms-transform": "translate3d(0px,0px,0px)",
            "-moz-transform": "translate3d(0px,0px,0px)"
        });
        
        $(".page_hide").css({
            "transition": "0.5s",
            "-webkit-transition": "0.5s",
            "-webkit-transform": "translate3d(100%, 0px, 0px)",
            "-o-transform": "translate3d(100%,0,0)",
            "-ms-transform": "translate3d(100%,0,0)",
            "-moz-transform": "translate3d(100%,0,0)"
        });
    } else if(typeSlider==="slidePrev"){
        // alert("prev");
        // alert("#page_"+url_target_back.replace("/", ""));
        $("#page_"+url_target_back.replace("/", "")).css({
            "transition": "0.5s",
            "-webkit-transition": "0.5s",
            "-webkit-transform": "translate3d(-100%, 0px, 0px)",
            "-o-transform": "translate3d(-100%,0,0)",
            "-ms-transform": "translate3d(-100%,0,0)",
            "-moz-transform": "translate3d(-100%,0,0)"
        }).removeClass("page_hide");
        
        $(".page_active").css({
            "transition": "0.5s",
            "-webkit-transition": "0.5s",
            "-webkit-transform": "translate3d(0%, 0px, 0px)",
            "-o-transform": "translate3d(0%,0,0)",
            "-ms-transform": "translate3d(0%,0,0)",
            "-moz-transform": "translate3d(0%,0,0)"
        });
    }
    else if(typeSlider==="prevNormal"){
        //   alert("prev normal");
        //page hide
        $("#page_"+url_target_back.replace("/", "")).css({
            "transition": "0.5s",
            "-webkit-transition": "0.5s",
            "-webkit-transform": "translate3d(-100%, 0px, 0px)",
            "-o-transform": "translate3d(-100%,0,0)",
            "-ms-transform": "translate3d(-100%,0,0)",
            "-moz-transform": "translate3d(-100%,0,0)"
       
        }).removeClass("page_hide");
        
        //page active
        $(".page_active").css({
            "transition": "0.5s",
            "-webkit-transition": "0.5s",
            "-webkit-transform": "translate3d(0%, 0px, 0px)",
            "-o-transform": "translate3d(0%,0,0)",
            "-ms-transform": "translate3d(0%,0,0)",
            "-moz-transform": "translate3d(0%,0,0)"
        });
    }
    else{//Next normal
        // alert("next normal");
        $(".page_active").css({
            "transition": "0s",
            "-webkit-transition": "0s",
            "-webkit-transform": "translate3d(0px, 0px, 0px)",
            "-o-transform": "translate3d(0px,0px,0px)",
            "-ms-transform": "translate3d(0px,0px,0px)",
            "-moz-transform": "translate3d(0px,0px,0px)"
        });
         
        $(".page_hide").css({
            "transition": "0s",
            "-webkit-transition": "0s",
            "-webkit-transform": "translate3d(100%, 0px, 0px)",
            "-o-transform": "translate3d(100%,0,0)",
            "-ms-transform": "translate3d(100%,0,0)",
            "-moz-transform": "translate3d(100%,0,0)"
        });
    }
  
    $.ajax({
        type:"GET",
        url:url,
        data:{},
        success:function(data){
            if(container.find("script").length==0||get_param("clear")=="page"){
                container.html(data);
            }
        }
    })
    .fail(function( jqXHR, textStatus ) {
        console.log( "Request failed: "+jqXHR.status + " - " + textStatus +" for "+url);
    });
}
/*
 *  Init variable target url
 */
url_target = "";
url_target_back = "";
isBack = false;
window.onpopstate = function(event) {
    url_target = window.location.hash.replace(/#|.html|tab-/g, "", "");
    tab = window.location.hash.indexOf("tab-");
  
    var index = url_target.indexOf("?");
    if (index > 0) {
        url_target = url_target.substring(0, parseInt(index));
    } 
    
    var id = "page_" + url_target.replaceAll("/", "");
	
    if (url_target === "") {
        /*
         * Trang được tải lên lần đầu
         */
        url_target = "control/index.html";
       
		
        if(isBack){//back ve trang chu
            load_page("app/"+url_target, $("#page_controlindex"),"slidePrev");
        }else{
            load_page("app/"+url_target, $("#page_controlindex"),"slideNext");
        }
    } else{

        
        if(tab>0){
            if(isBack){
                load_page("app/"+url_target+".html", $("#" + id),"prevNormal");
            }else{
                load_page("app/"+url_target+".html", $("#" + id),"nextNormal");
            }
        }else{
            if(isBack){
                // alert(url_target_back);
                load_page("app/"+url_target+".html", $("#" + id),"slidePrev");
            }else{
                //alert(url_target_back);
                load_page("app/"+url_target+".html", $("#" + id),"slideNext");
            }
            
        }
    }
    url_target_back = url_target;
    isBack=false;
};

$(function() {
    $.ajaxSetup({
        cache: false,
        async: true
    });
    $(".page_item").css({
        "transition": "0s",
        "-webkit-transition": "0s",
        "-webkit-transform": "translate3d(-100%, 0px, 0px)",
        "-o-transform": "translate3d(-100%,0,0)",
        "-ms-transform": "translate3d(-100%,0,0)",
        "-moz-transform": "translate3d(-100%,0,0)"
    });

    /*
     * Hiển thị trang hiện tại
     */
    $(".page_active").css({
        "transition": "0s",
        "-webkit-transition": "0s",
        "-webkit-transform": "translate3d(0px, 0px, 0px)",
        "-o-transform": "translate3d(0px,0px,0px)",
        "-ms-transform": "translate3d(0px,0px,0px)",
        "-moz-transform": "translate3d(0px,0px,0px)"
    });
    
    
    /*
     * Back button click event
     */
    $(document).on("tap", ".btn-back, .back_even", function() {
        isBack = true;
        window.history.back();
    });
    
});

function redirect_back(){
    isBack=true;
    window.history.back();
}